<template>
    <div class="register_warp">
        <div class="register">
            <div class="re_title">用户注册</div>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" >
            <el-form-item label="用户名" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass" required>
                <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="repwd" required>
                <el-input type="password" v-model="ruleForm.repwd" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="手机号码" prop="mobile" required>
                <el-input v-model="ruleForm.mobile"></el-input>
            </el-form-item>
            <el-form-item
                prop="email"
                label="邮箱"
                :rules="[
                { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
                ]"
            >
            <el-input v-model="ruleForm.email"></el-input>
            </el-form-item>
            <!-- <el-form-item label="年龄" prop="age">
                <el-input v-model.number="ruleForm.age"></el-input>
            </el-form-item> -->
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
  export default {
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.repwd !== '') {
            this.$refs.ruleForm.validateField('repwd');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          name:'',
          pass: '',
          repwd: '',
          email: '',
          mobile:''
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          repwd: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          name: [
            { required: true, message: '请输入用户名', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '请输入手机号', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // 提交————》跳转
            this.submitdata()
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    submitdata(){
      var qs=require('qs');
      var instance_all = this.$http.create({ headers: {'content-type': 'application/x-www-form-urlencoded'} });
        instance_all.post('http://blog.com:80/home/api/register',qs.stringify(this.ruleForm)).then((result) => {
        let yz = result.data.status
        if(yz == 4001){
          this.$message.error('用户名已存在，请更换重试！');
        }else if(yz == 200){
          this.$message.success('注册成功！')
          this.$router.push({ path: '/login', })
        }else{
          this.$message.error('未知错误，请刷新重试！');
        }
      }).catch((err) => {
          this.$message.error(err);
      })
      }
      
    
    }
  }
</script>

<style>
.register_warp{
    position: relative;
    height: 600px;
    background: url('../assets/login_img.png') no-repeat;
}
.register{
    width: 30%;
    position: absolute;
    top: 60px;
    left: 50%;
    padding-right: 30px;
    background-color: #f7f3f3;
    border-radius: 6px;
}
.re_title{
    font-size: 17px;
    padding: 16px 0 4px 8px;
    text-align: left;
    border-bottom: 2px solid #e0dfdf;
    margin-bottom: 22px;
}
</style>